using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
public class BattleTestInitializer : MonoBehaviour
{
    public EnemyConfig enemyConfig;
    public BattleManager battleManager;
    public CardManager cardManager;
    public PlayerData player;
    public EnemyData targetEnemy;
    // Start is called before the first frame update
    void Awake()
    {
        // 获取组件和数据
        if (battleManager == null) 
        {
            battleManager = FindObjectOfType<BattleManager>();
        }
        
        if (cardManager == null)
        {
            cardManager = FindObjectOfType<CardManager>();
        }

        if (player == null)
        {
            //player = FindObjectOfType<PlayerData>();
        }

        if (targetEnemy == null)
        {
            targetEnemy = FindObjectOfType<EnemyData>();
        }
        //ReinitializeComponents();
        // 检查所有依赖是否找到
        if (battleManager == null || cardManager == null || player == null || targetEnemy == null)
        {
            Debug.LogError("战斗核心组件或测试数据未找到，请检查场景配置。");
            return;
        }
        // 仅查找组件，不要在这里调用 StartBattle
    }

    void Start()
    {
        // 在 Start() 中启动战斗，确保所有组件的 Awake/Start 都已完成
        // 收集所有敌人
        List<EnemyData> enemies = new List<EnemyData>();

        // 调用BattleManager的StartBattle方法开始战斗
        Debug.Log("--- BattleTestInitializer: 启动战斗 ---");
        BattleLoader.CopyPlayerDataTo(player);
        targetEnemy.InitializeFromConfig(enemyConfig.GetEnemyData(BattleLoader.battleConfig.enemyIds[0]));
        enemies.Add(targetEnemy);
        battleManager.StartBattle(player, enemies);
    }

    [ContextMenu("重新初始化组件")]
    public void ReinitializeComponents()
    {
        battleManager = FindObjectOfType<BattleManager>();
        cardManager = FindObjectOfType<CardManager>();
        player = FindObjectOfType<PlayerData>();
        targetEnemy = FindObjectOfType<EnemyData>();

        Debug.Log("组件已重新初始化");
    }
    // -----------------------------------------------------------------
    // 调试辅助方法：在运行时点击 Inspector 窗口的三个点（...）进行调用
    // -----------------------------------------------------------------

    [ContextMenu("1. 模拟玩家回合结束")]
    public void SimulateEndTurn()
    {
        if (battleManager != null)
        {
            Debug.Log("\n[DEBUG] === 模拟玩家结束回合 ===");
            battleManager.EndPlayerTurn();
        }
    }
    
    public void SimulateUseStrike(Card card)
    {
        //ReinitializeComponents();
        // 模拟 UI 行为：找到一张牌，并找到目标
        if (cardManager == null )
        {
            Debug.LogWarning("[DEBUG] 未找到cardManager");
            return;
        }
        if(targetEnemy == null)
        {
            Debug.LogWarning("[DEBUG] targetEnemy");
            return;
        }
        // 尝试从手牌中找到打击牌
        Card strikeCard = card;
        Debug.Log("已找到打击牌");

        if (strikeCard != null)
        {
            Debug.Log($"[DEBUG] === 模拟使用卡牌: {strikeCard.name} (目标: {targetEnemy.name}) ===");
            // 调用 BattleManager 暴露的接口
            battleManager.PlayerUsedCard(strikeCard, targetEnemy);
        }
        else
        {
            Debug.LogWarning("[DEBUG] 手牌中没有打击牌，无法模拟使用。请先结束回合或抽牌。");
        }
    }
    
    
    public void SimulateUseDefend(Card card)
    {
        //ReinitializeComponents();
        if (cardManager == null) return;
        Card defendCard = card;
        
        if (defendCard != null)
        {
            Debug.Log($"[DEBUG] === 模拟使用卡牌: {defendCard.name} (目标: Player) ===");
            // 防御牌目标是自己 (player)
            battleManager.PlayerUsedCard(defendCard, player);
        }
        else
        {
            Debug.LogWarning("[DEBUG] 手牌中没有防御牌，无法模拟使用。");
        }
    }

    
    public void SimulateUseBash(Card card)
    {
        //ReinitializeComponents();
        // 模拟 UI 行为：找到一张牌，并找到目标
        if (cardManager == null || targetEnemy == null) return;

        // 尝试从手牌中找到打击牌
        Card bashCard = card;

        if (bashCard != null)
        {
            Debug.Log($"[DEBUG] === 模拟使用卡牌: {bashCard.name} (目标: {targetEnemy.name}) ===");
            // 调用 BattleManager 暴露的接口
            battleManager.PlayerUsedCard(bashCard, targetEnemy);
        }
        else
        {
            Debug.LogWarning("[DEBUG] 手牌中没有重击牌，无法模拟使用。请先结束回合或抽牌。");
        }
    }
}
